import { Request, Response } from 'express'
import { pool } from '../db'
import { ResultSetHeader, RowDataPacket } from 'mysql2'

export class PromptController {
  // 获取用户的所有提示词
  async getUserPrompts(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      const [rows] = await pool.execute<RowDataPacket[]>(
        'SELECT * FROM prompts WHERE created_by = ? AND status = 1 ORDER BY is_default DESC',
        [userId]
      )
      res.json({ code: 0, data: rows })
    } catch (error) {
      res.status(500).json({ code: 500, message: '获取提示词列表失败', error })
    }
  }

  // 获取默认提示词
  async getDefaultPrompt(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      const [rows] = await pool.execute<RowDataPacket[]>(
        'SELECT * FROM prompts WHERE created_by = ? AND is_default = 1 AND status = 1 LIMIT 1',
        [userId]
      )
      
      if (rows.length === 0) {
        return res.status(404).json({ code: 404, message: '未找到默认提示词' })
      }

      res.json({ code: 0, data: rows[0] })
    } catch (error) {
      res.status(500).json({ code: 500, message: '获取默认提示词失败' })
    }
  }

  // 创建提示词
  async createPrompt(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      const { name, content, isDefault } = req.body

      // 如果设置为默认,先将其他提示词设为非默认
      if (isDefault) {
        await pool.execute(
          'UPDATE prompts SET is_default = 0 WHERE created_by = ?',
          [userId]
        )
      }

      const [result] = await pool.execute<ResultSetHeader>(
        'INSERT INTO prompts (name, content, is_default, created_by, status) VALUES (?, ?, ?, ?, 1)',
        [name, content, isDefault, userId]
      )

      res.json({
        code: 0,
        data: { id: result.insertId }
      })
    } catch (error) {
      res.status(500).json({ code: 500, message: '创建提示词失败' })
    }
  }

  // 更新提示词
  async updatePrompt(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      const promptId = req.params.id
      const { name, content, isDefault } = req.body

      // 如果设置为默认,先将其他提示词设为非默认
      if (isDefault) {
        await pool.execute(
          'UPDATE prompts SET is_default = 0 WHERE created_by = ?',
          [userId]
        )
      }

      const [result] = await pool.execute<ResultSetHeader>(
        'UPDATE prompts SET name = ?, content = ?, is_default = ? WHERE id = ? AND created_by = ?',
        [name, content, isDefault, promptId, userId]
      )

      if (result.affectedRows === 0) {
        return res.status(404).json({ code: 404, message: '提示词不存在或无权限修改' })
      }

      res.json({ code: 0, message: '更新成功' })
    } catch (error) {
      res.status(500).json({ code: 500, message: '更新提示词失败' })
    }
  }

  // 删除提示词 (硬删除)
  async deletePrompt(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      const promptId = req.params.id

      // 硬删除 - 真正从数据库中删除数据
      const [result] = await pool.execute<ResultSetHeader>(
        'DELETE FROM prompts WHERE id = ? AND created_by = ?',
        [promptId, userId]
      )

      if (result.affectedRows === 0) {
        return res.status(404).json({ code: 404, message: '提示词不存在或无权限删除' })
      }

      res.json({ code: 0, message: '删除成功' })
    } catch (error) {
      res.status(500).json({ code: 500, message: '删除提示词失败' })
    }
  }

  // 设置默认提示词
  async setDefaultPrompt(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      const promptId = req.params.id

      await pool.execute(
        'UPDATE prompts SET is_default = 0 WHERE created_by = ?',
        [userId]
      )

      const [result] = await pool.execute<ResultSetHeader>(
        'UPDATE prompts SET is_default = 1 WHERE id = ? AND created_by = ?',
        [promptId, userId]
      )

      if (result.affectedRows === 0) {
        return res.status(404).json({ code: 404, message: '提示词不存在或无权限设置' })
      }

      res.json({ code: 0, message: '设置成功' })
    } catch (error) {
      res.status(500).json({ code: 500, message: '设置默认提示词失败' })
    }
  }
} 